package main

/*
*	haozibi
*	b@ydo.io
*	http://b.ydo.io
*	2016-9-9
 */

import (
	"crypto/md5"
	"encoding/hex"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"time"
)

func getFilelist(path string) int {
	num := 0
	err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
		if f == nil {
			return err
		}
		if f.IsDir() {
			return nil
		}
		//fmt.Printf("%v -> %v\n", path, fileMd5(path))
		fmt.Printf("%v -> %c[1;40;32m%v%c[0m\n", path, 0x1B, fileMd5(path), 0x1B)
		num = num + 1
		return nil
	})
	if err != nil {
		fmt.Printf("\n ERROR : \n filepath.Walk() returned %v\n", err)
	}
	return num
}

func fileMd5(fileName string) string {
	Buf, buferr := ioutil.ReadFile(fileName)
	if buferr != nil {
		fmt.Println(fileName, buferr)
	}
	md5Ctx := md5.New()
	md5Ctx.Write(Buf)
	cipherStr := md5Ctx.Sum(nil)
	return hex.EncodeToString(cipherStr)
}

func main() {
	t1 := time.Now()
	flag.Parse()
	root := flag.Arg(0)
	num := getFilelist(root)
	fmt.Printf("\n%5v : %v\n%5v : %v\n", "Num", num, "Time", time.Now().Sub(t1))
}
